<?php
/**
* Letterman Newsletter Component
* 
* @package Letterman
* @author Soeren
* @copyright Soeren Eberhardt <soeren@virtuemart.net>
*   (who just needed an easy and *working* Newsletter component for Mambo 4.5.1 and mixed up Newsletter and YaNC)
* @copyright Mark Lindeman <mark@pictura-dp.nl> 
*    (parts of the Newsletter component by Mark Lindeman; Pictura Database Publishing bv, Heiloo the Netherland)
* @copyright Adam van Dongen <adam@tim-online.nl>
*    (parts of the YaNC component by Adam van Dongen, www.tim-online.nl)
*    
* @copyright Philip Sylvain <contact@sphilip.com>
*  	(MVC refactorization and AJAX)
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*
*/



function dump($var, $label=null, $echo=true){
        // format the label
        $label = ($label===null) ? '' : rtrim($label) . ' ';

        // var_dump the variable into a buffer and keep the output
        ob_start();
        var_dump($var);
        $output = ob_get_clean();

        // neaten the newlines and indents
        $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
        if (PHP_SAPI == 'cli') {
            $output = PHP_EOL . $label
                    . PHP_EOL . $output
                    . PHP_EOL;
        } else {
            $output = '<pre>'
                    . $label
                    . htmlentities($output, ENT_QUOTES, 'UTF-8')
                    . '</pre>';
        }

        if ($echo) {
            echo($output);
        }
        return $output;
}

class Com_Letterman
{
    
    function &getParams()
    {
		static $instance;
        
		if ( !is_object($instance) ) {
		    
		    $instance =& JComponentHelper::getParams('com_letterman');
		    
		    //Sets a default values if not already assigned
		    $instance->def('use_ajax', 1);
		    $instance->def('smtp_method', 0);
		    $instance->def('smtp_auth', 0);
		    $instance->def('smtp_host', '');
		    $instance->def('smtp_user', '');
		    $instance->def('smtp_password', '');
		    $instance->def('sendmail_method', 0);
		    $instance->def('sendmail_path', ini_get('sendmail_path'));
		    $instance->def('php_method', 1);
		    $instance->def('charset', 'utf-8');
		    $instance->def('disable_timeout', 1);
		    $instance->def('mails_per_step', 50);
		}

		return $instance ;
    }
    
    function runSite()
    {
		// Require the base controller
        require_once(JPATH_COMPONENT.DS.'controller.php');
		
		// Component Helper
		jimport('joomla.application.component.helper');
		
		// Create the controller
		$controller = new LettermanController();
		
		// Perform the Request task
		$controller->execute(JRequest::getVar('task', null, 'default', 'cmd'));
		$controller->redirect();
        
    }
    
    function runAdmin()
    {
	
		// Require specific controller if requested
		if( $controller = JRequest::getWord('controller', 'newsletters') ) {
			
			$path = JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php';
			
			if ( file_exists($path) ) {
				require_once $path;
			} else {
				$controller = '';
			}
		}
		
		// Create the controller
		$classname	= 'LettermanController'.ucfirst($controller);
		$controller	= new $classname( );
		
		// Perform the Request task
		$controller->execute( JRequest::getCmd( 'task' ) );
		$controller->redirect();
    	
    	
    	
    }
    
    
    
}
